#ifndef __LM_FAST_STL_H__
#define __LM_FAST_STL_H__

#include "lm_base.h"

using namespace std;

#define PERF_MW_MSGS 128


template<class T>
class lmVector
{
	public:
		lmVector(int nums = 8, char mode)
		{
		    count   = max_count = 0;
			nb_msgs = nums;
			m_mode  = mode;
		    pmsgs   = new T*[nb_msgs];
			
			if(m_mode)
			{
				int i = 0;
				T* tmsg = NULL;
				for(; i < nb_msgs; i++)
				{
					tmsg     = new T(); 
					pmsgs[i] = tmsg;
				}
			}
			LOGE("=====init_create=pmsgs=[%p]====\n", pmsgs);
		}
		
		~lmFastVector()
		{
			free();
			LOGE("=====bbbb==\n");
   		 	FREE_OBJAARY(pmsgs);
			LOGE("===pmsgs:[%p]==\n", pmsgs);
		}
		
	public:
		 int dataNums(){ return count;} 
		 int begin(){ return 0;}
		 int end(){ return count;}
		 T** getData(){ return pmsgs;}
		 void clearData(){ count = 0; LOGE("=====clear=====\n");}
		 void setMaxSize(uint max){ max_count = max; }
		  
		 T* at(uint index)
		 {
			  RETU_CONDX(index > count, NULL);
		     return pmsgs[index];
		 }
		 
		 int eraseData(int _begin, int _end)
		 {
		 	RETU_CONDX(m_mode, );
			RETU_CONDX(_begin > _end, -1);
		    uint size = _end - _begin; 
			//printf("--ttt--[%d]-[%d->\n", count, size);
			count -= size;
			RETU_CONDX(count <= 0, -1);
			
			//printf("--uuu--[%d]-[%d->\n", count, size);
		    for (uint i = 0; i < count; i++) {
				 //LOGE("=erase=[%p]=[%p]==\n", pmsgs[_begin + i], pmsgs[_end + i]);
		        pmsgs[_begin + i] = pmsgs[_end + i];
		    }
		    //LOGE("=====erase=====\n");
		   
			return 0;
		  }

		  //way 1. push data, use m_mode 1.
		  int get_push_buff(T *&pst)
		  {
		   	  RETU_CONDX(!m_mode, -1);
			  if(pmsgs)
			  {
				 if(count >= nb_msgs) 
				 {
				     int size = nb_msgs * 2;
				     T **buf  = new T*[size], *tmsg = NULL;
				     for (int i = 0; i < size; i++) 
					 {
					 	if(i < nb_msgs){
				        	 buf[i] = pmsgs[i];
					 	}
						else{
							tmsg   = new T(); 
							buf[i] = tmsg;
						}
				     }
				     PRT_INF("fast vector incrase %d==>%d", nb_msgs, size);
				     
				     //use new array.
				     FREE_OBJ(pmsgs);
				     pmsgs   = buf;
				     nb_msgs = size;
				 }
			  }
				 
			  pst = pmsgs[count++];
			  return 0;
		  }

		  //way 2. push data. use m_mode 0.
		  int push_back(T* msg)
		  {
		  	  RETU_CONDX(m_mode, --1);
			  if(pmsgs)
			  {
				 if(count >= nb_msgs) 
				 {
				     int size =  nb_msgs * 2;
				     T** buf = new T*[size];
				     for (int i = 0; i < nb_msgs; i++) {
				         buf[i] = pmsgs[i];
				     }
				     PRT_INF("fast vector incrase %d==>%d", nb_msgs, size);
				     
				     // use new array.
				     FREE_OBJ(pmsgs);
				     pmsgs   = buf;
				     nb_msgs = size;
				 }

				// printf("=push_back=[%d]=[%d]=[%p]==\n", count, nb_msgs, msg);
				pmsgs[count++] = msg;
			 }
		   }
		 
		   void free()
		   {
		      for (int i = 0; i < count; i++) 
			  {
		          T* msg = pmsgs[i];
				  LOGE("=====free=[%p]====\n", msg);
		          FREE_OBJ(msg);
				  LOGE("=====free3333=[%p]====\n", msg);
		       }
			  
		       count = 0;
		    }
	
     private:
	     T**  pmsgs;
	     int  nb_msgs;
		 int  max_count;
	     int  count;
		 char m_mode; //0: use upper allocated memory;   1:use lmVector allocated memory;
};


typedef template<class T> class lmVector  lmFastVector;

#endif

